package com.ibiaoke.boss.controller;

import com.ibiaoke.boss.entity.SysUser;
import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.order.entity.GoodsRush;
import com.ibiaoke.order.entity.vo.GoodsRushVO;
import com.ibiaoke.order.service.GoodsRushService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class BossGoodsRushController extends BaseController{

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    @Autowired
    private GoodsRushService goodsRushService;

    /**
     * 添加抢购活动
     * @param goodsRushVO
     * @param result
     * @return
     */
    @RequestMapping("/saveGoodsRush")
    @ResponseBody
    public Res saveGoodsRush(@Validated GoodsRushVO goodsRushVO, BindingResult result) {
        SysUser user = getLoginUser();
        handleValidate(result);
        goodsRushService.saveGoodsRush(goodsRushVO);
        logger.info("系统用户{}操作了添加抢购活动的操作",user.getUserName());
        return Res.create();
    }

    /**
     * 查询所有未过期的抢购活动
     * @return
     */
    @RequestMapping("/listGoodsRush")
    @ResponseBody
    public Res listGoodsRush() {
        SysUser user = getLoginUser();
        List<GoodsRush> goodsRushList = goodsRushService.listALLRush(null);
        logger.info("系统用户{}操作了查询所有未过期的抢购活动的操作",user.getUserName());
        return Res.create().addResponse("goodsRushList", goodsRushList);
    }

    /**
     * 修改抢购活动
     * @param goodsRushVO
     * @return
     */
    @RequestMapping("/updateGoodsRush")
    @ResponseBody
    public Res updateGoodsRush(GoodsRushVO goodsRushVO) {
        SysUser user = getLoginUser();
        goodsRushService.updateGoodsRush(goodsRushVO);
        logger.info("系统用户{}操作了修改抢购活动的操作",user.getUserName());
        return Res.create();
    }

    /**
     * 根据抢购id来删除抢购活动
     * @return
     */
    @RequestMapping("/deleteGoodsRush")
    @ResponseBody
    public Res deleteGoodsRush(Integer rushId) {
        SysUser user = getLoginUser();
        goodsRushService.deleteGoodsRush(rushId);
        logger.info("系统用户{}操作了删除抢购活动的操作",user.getUserName());
        return Res.create();
    }
}
